
const getMenuTree = (array = [], upKey, filterTypeList = []) => {

    let list = []
    if (array.length === 0) return list;
    // 除去parentKey节点不相等的剩余节点
    const arr = array.filter((item, index) => {
        const {
            id = undefined,
            activeMenuId = undefined,
            visible = true,
            parentId = String,
            menuName = String,
            menuType = String,
            menuIcon = String,
            routerPath = String,
            sort = Number,
            componentName = String,
            componentPath = String,
            permissionMark = String,
            status = Number
        } = item;

        if (parentId !== upKey && (!filterTypeList.includes(menuType))) {
            return {
                id: id,
                activeMenuId,
                visible,
                parentId,
                menuName,
                menuType,
                menuIcon,
                routerPath,
                sort,
                componentName,
                componentPath,
                permissionMark,
                status
            }
        }
    })
    array.forEach((item, i) => {
        const {
            id,
            activeMenuId = String,
            visible: visible,
            parentId = String,
            menuName = String,
            menuType = String,
            menuIcon = String,
            routerPath = String,
            sort = Number,
            componentName = String,
            componentPath = String,
            permissionMark = String,
            status = Number
        } = item;
        if (parentId === upKey && (!filterTypeList.includes(menuType))) {
            list.push({
                id: id,
                parentId,
                activeMenuId,
                visible,
                menuName,
                menuType,
                menuIcon,
                routerPath,
                sort,
                componentName,
                componentPath,
                permissionMark,
                status,
                children: getMenuTree(arr, id)
            })
        }
    })
    return list;
}



const getAuthorityList = (arr, upKey) => {

    let list = arr.filter(item => {
        if (item.parentId === upKey && item.menuType === "3") {
            return {
                ...item,
            };
        }
    });
    return list;
}
const getPermissionConfigTree = (array, upKey) => {
    let list = []
    // 除去parentId节点不相等且不是menu的剩余节点
    const arr = array.filter((item, index) => {
        // console.log("遍历 ：item ：", item)
        const { id, parentId, menuName, visible, menuType, menuIcon, routePath, sort, componentName, componentPath, permissionMark, status } = item;
        if (parentId !== upKey && visible === true) {
            return {
                id, parentId, menuName, visible, menuType, menuIcon, routePath, sort, componentName, componentPath, permissionMark, status

            }
        }
    })

    // 剩下的节点 父节点相等，
    array.forEach((item, i) => {

        const { id, parentId, menuName, menuType, visible, menuIcon, routePath, sort, componentName, componentPath, permissionMark, status } = item;

        if (parentId === upKey && (menuType === "1" || menuType === "2" || menuType === "4") && visible === true) {
            list.push({
                id, parentId, menuName, menuType, menuIcon, visible, routePath, sort, componentName, componentPath, permissionMark, status,
                children: getPermissionConfigTree(arr, id).length > 0 ? getPermissionConfigTree(arr, id) : undefined,
                authorityList: getAuthorityList(array, id)
            })
        }
    })

    return list;

}




// 过滤数据
const getMenuTreeForPermission = (allTree, permissionList) => {
    const filter = (list) => {
        let res = [];
        list.forEach(item => {
            if (item.children.length > 0) {
                res.push({
                    ...item,
                    children: filter(item.children)
                })
            }
            permissionList.includes(item.permissionMark) && res.push(item);
        })
        return res;
    }
    let res = filter(allTree);
    return res;

}



module.exports = {
    getMenuTree,
    getPermissionConfigTree,
    getMenuTreeForPermission
}
